package com.bes.opslink.aiops.commons.exepction.handler;

import com.bes.opslink.aiops.commons.dto.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理默认的异常信息
     *
     * @param e 异常信息
     * @return 错误处理后的统一响应报文
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public R handleException (Exception e) {
        log.error(e.getMessage(), e);

        return R.builder()
                .message(e.getMessage())
                .code(R.FAIL_CODE)
                .build();
    }
}
